package com.tarena.sentinel.demo02.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.sentinel.demo02.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    @SentinelResource(value="resource01",blockHandler ="sayHiError" )
    public String sayHi(String name){
        String result=helloService.sayHi(name);
        return result;
    }
    //注解添加blockHandler 值指向当前类的一个同名方法
    //方法就作为底层调用目标方法失败时 降级处理逻辑.
    //降级方法必须保证和目标方法参数一致,多一个参数异常
    //降级方法的返回值也要和目标方法一致
    public String sayHiError(String name,BlockException error){
        log.info("当前异常类型:"+error.getClass().getName());
        return "blockHandler:"+name+" sorry.";
    }
}
